﻿

Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_RaoVat_RaoVat
    Inherits System.Web.UI.UserControl
    Dim intLevel As Integer = 0
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadData()
        End If
    End Sub
    Private Sub LoadData()
        Using Data As New VSEntities
            Dim p = (From q In Data.Categories Where q.MainCate = True Order By q.Sort Ascending Select q.CatId, q.Title, q.Description, q.QueryString).ToList
            rptCate.DataSource = p
            rptCate.DataBind()
        End Using
    End Sub

    Protected Sub rptCate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCate.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim rptSubCate As Repeater = CType(e.Item.FindControl("rptSubCate"), Repeater)
        Dim hplTile As HyperLink = CType(e.Item.FindControl("hplTile"), HyperLink)
        Dim hidCateID As HiddenField = CType(e.Item.FindControl("hidCateID"), HiddenField)
        Dim ltrTab As Literal = CType(e.Item.FindControl("ltrTab"), Literal)
        Dim hplMore As HyperLink = CType(e.Item.FindControl("hplMore"), HyperLink)
        hidCateID.Value = row.CatId.ToString



        hplTile.Text = row.Title
        hplTile.NavigateUrl = MainDomain & "rao-vat/" & row.QueryString & "-" & row.CatId.ToString & ".html"

        hplTile.Attributes.Add("style", "cursor:pointer")

        Dim rptItem As Repeater = CType(e.Item.FindControl("rptItem"), Repeater)

        Using Data As New VSEntities
            'Lay toan bo cap con cua Cate
            Dim a = (From r In Data.uspSelectSubCate(hidCateID.Value) Select r.CateID).ToArray
            Dim p = (From q In Data.RaoVats Where (a.Contains(q.CateID) Or q.CateID = hidCateID.Value) And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending
                    Select New With {q.RaovatId, q.Title, q.Description, q.QueryString, q.ImagePreview, q.SellPrice, q.OriginalPrice, .CateQuerystring = q.Category.QueryString, q.CateID}).Take(4).ToList
            rptItem.DataSource = p
            rptItem.DataBind()
        End Using

        'Dim h = (From q In Data.Categories Where q.CatId = hidCateID.Value Order By q.CatId Descending Select q.CatId, q.Title, q.Description, q.QueryString).ToList
        'For i As Integer = 0 To h.Count - 1
        '    ltrTab.Text &= "<li><a href='#tabsporlet" & intLevel.ToString & "_" & i.ToString & "'>" & h.Item(i).Title & "</a></li>"
        'Next
        'rptSubCate.DataSource = h
        'rptSubCate.DataBind()

        'Using Data As New VSEntities
        '    Dim p = (From q In Data.Categories Where q.ParentId = hidCateID.Value Order By q.CatId Descending Select q.CatId, q.Title, q.Description, q.QueryString).ToList
        '    If p Is Nothing Or p.Count = 0 Then
        '        Dim h = (From q In Data.Categories Where q.CatId = hidCateID.Value Order By q.CatId Descending Select q.CatId, q.Title, q.Description, q.QueryString).ToList
        '        For i As Integer = 0 To h.Count - 1
        '            ltrTab.Text &= "<li><a href='#tabsporlet" & intLevel.ToString & "_" & i.ToString & "'>" & h.Item(i).Title & "</a></li>"
        '        Next
        '        rptSubCate.DataSource = h
        '        rptSubCate.DataBind()
        '    Else
        '        'For i As Integer = 0 To p.Count - 1
        '        '    ltrTab.Text &= "<li><a href='#tabsporlet" & intLevel.ToString & "_" & i.ToString & "'>" & p.Item(i).Title & "</a></li>"
        '        'Next
        '        rptSubCate.DataSource = p
        '        rptSubCate.DataBind()
        '    End If
        'End Using
        hplMore.NavigateUrl = MainDomain & "rao-vat/" & row.QueryString & "-" & row.CatId.ToString & ".html"

    End Sub
    'Protected Sub rptSubCate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
    '    Dim row = CType(e.Item.DataItem, Object)
    '    Dim rptItem As Repeater = CType(e.Item.FindControl("rptItem"), Repeater)
    '    Dim hidSubCateID As HiddenField = CType(e.Item.FindControl("hidSubCateID"), HiddenField)
    '    Dim ltrHead As Literal = CType(e.Item.FindControl("ltrHead"), Literal)
    '    Dim hplMore As HyperLink = CType(e.Item.FindControl("hplMore"), HyperLink)

    '    hidSubCateID.Value = row.CatId.ToString

    '    'ltrHead.Text = "<div id='tabsporlet" & intLevel.ToString & "_" & e.Item.ItemIndex.ToString & "'>"
    '    Using Data As New VSEntities
    '        'Lay toan bo cap con cua Cate
    '        Dim a = (From r In Data.uspSelectSubCate(hidSubCateID.Value) Select r.CateID).ToArray
    '        Dim p = (From q In Data.RaoVats Where (a.Contains(q.CateID) Or q.CateID = hidSubCateID.Value) And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending
    '                Select New With {q.RaovatId, q.Title, q.Description, q.QueryString, q.ImagePreview, q.SellPrice, q.OriginalPrice, .CateQuerystring = q.Category.QueryString, q.CateID}).Take(4).ToList
    '        rptItem.DataSource = p
    '        rptItem.DataBind()
    '    End Using
    '    hplMore.NavigateUrl = MainDomain & "rao-vat/" & row.QueryString & "-" & row.CatId.ToString & ".html"
    'End Sub
    Protected Sub rptItem_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row = CType(e.Item.DataItem, Object)
        Dim hplImg As HyperLink = CType(e.Item.FindControl("hplImg"), HyperLink)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        Dim ltrPrice As Literal = CType(e.Item.FindControl("ltrPrice"), Literal)
        ltrPrice.Text = FormatNumber(row.OriginalPrice.ToString)

        img.ImageUrl = CookedImageURL("140-120", row.ImagePreview.ToString)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & "rao-vat-chi-tiet/" & row.QueryString & "-" & row.RaovatId.ToString & ".html"
        hplImg.NavigateUrl = MainDomain & "rao-vat-chi-tiet/" & row.QueryString & "-" & row.RaovatId.ToString & ".html"
    End Sub

End Class
